/**
 * Copyright (c) 2018-2022, NXOS Development Team
 * SPDX-License-Identifier: Apache-2.0
 *
 * Contains: IPC
 *
 * Change Logs:
 * Date           Author            Notes
 * 2023-07-23     kangjinci           Init
 */

#pragma once

#include <nxos/fs.h>
#include <nxos/list.h>
#include <nxos/defines.h>
#include <nxos/types.h>

void Do_VFS_Init(NX_Size buf_size);

typedef enum VFS_ReqType
{
    VFS_REQ_INVALID,
    VFS_REQ_MOUNT,
    VFS_REQ_UNMOUNT,
    VFS_REQ_SYNC,
    VFS_REQ_OPEN,
    VFS_REQ_CLOSE,
    VFS_REQ_READ,
    VFS_REQ_WRITE,
    VFS_REQ_IOCTL,
    VFS_REQ_LSEEK,
    VFS_REQ_FSYNC,
    VFS_REQ_FCHMOD,
    VFS_REQ_FSTAT,
    VFS_REQ_OPENDIR,
    VFS_REQ_CLOSEDIR,
    VFS_REQ_READDIR,
    VFS_REQ_REWINDDIR,
    VFS_REQ_MKDIR,
    VFS_REQ_RMDIR,
    VFS_REQ_RENAME,
    VFS_REQ_UNLINK,
    VFS_REQ_ACCESS,
    VFS_REQ_CHMOD,
    VFS_REQ_STAT,
} VFS_ReqType;

typedef struct VFS_IpcReq
{
    VFS_ReqType type;
    union
    {
        struct VFS_MountArgs
        {
            char dev[NX_FILE_MAX_PATH];
            char dir[NX_FILE_MAX_PATH];
            char fsname[32];
            NX_U32 flags;
        } vfs_mount_args;
        struct VFS_UnmountArgs
        {
            char path[NX_FILE_MAX_PATH];
        } vfs_unmount_args;
        struct VFS_SyncArgs
        {
            NX_U32 dummy;
        } vfs_sync_args;
        struct VFS_OpenArgs
        {
            char path[NX_FILE_MAX_PATH];
            NX_U32 flags;
            NX_U32 mode;
        } vfs_open_args;

        struct VFS_CloseArgs
        {
            NX_Solt solt;
        } vfs_close_args;

        struct VFS_ReadArgs
        {
            NX_Solt solt;
            NX_Size len;
            char buf[0];
        } vfs_read_args;

        struct VFS_WriteArgs
        {
            NX_Solt solt;
            NX_Size len;
            char buf[0];
        } vfs_write_args;

        struct VFS_IoctlArgs
        {
            NX_Solt solt;
            NX_U32 cmd;
            void *arg;
        } vfs_ioctl_args;

        struct VFS_LseekArgs
        {
            NX_Solt solt;
            NX_Offset off;
            int whence;
        } vfs_lseek_args;

        struct VFS_FsyncArgs
        {
            NX_Solt solt;
        } vfs_fsync_args;

        struct VFS_FchmodArgs
        {
            NX_Solt solt;
            NX_U32 mode;
        } vfs_fchmod_args;

        struct VFS_FstatArgs
        {
            NX_Solt solt;
            NX_FileStatInfo st;
        } vfs_fstat_args;

        struct VFS_OpenDirArgs
        {
            char name[NX_FILE_MAX_PATH];
        } vfs_opendir_args;

        struct VFS_CloseDirArgs
        {
            NX_Solt solt;
        } vfs_closedir_args;

        struct VFS_ReadDirArgs
        {
            NX_Solt solt;
            NX_Dirent dir;
        } vfs_readdir_args;

        struct VFS_RewindDirArgs
        {
            NX_Solt solt;
        } vfs_rewinddir_args;

        struct VFS_MkdirArgs
        {
            char path[NX_FILE_MAX_PATH];
            NX_U32 mode;
        } vfs_mkdir_args;

        struct VFS_RmDirArgs
        {
            char path[NX_FILE_MAX_PATH];
        } vfs_rmdir_args;

        struct VFS_RenameArgs
        {
            char src[NX_FILE_MAX_PATH];
            char dst[NX_FILE_MAX_PATH];
        } vfs_rename_args;

        struct VFS_UnlinkArgs
        {
            char path[NX_FILE_MAX_PATH];
        } vfs_unlink_args;

        struct VFS_AccessArgs
        {
            char path[NX_FILE_MAX_PATH];
            NX_U32 mode;
        } vfs_access_args;

        struct VFS_ChmodArgs
        {
            char path[NX_FILE_MAX_PATH];
            NX_U32 mode;
        } vfs_chmod_args;

        struct VFS_StatArgs
        {
            char path[NX_FILE_MAX_PATH];
            NX_FileStatInfo st;
        } vfs_stat_args;
    };

} VFS_IpcReq;

#define GET_FS_REQUEST_SIZE(tag) \
    (NX_OFFSET_OF_STRUCT(VFS_IpcReq, vfs_##tag##_args) + sizeof(((VFS_IpcReq *)0)->vfs_##tag##_args))

/**
 * client api
 */
#if 0

NX_Error NX_FileSystemMount(const char * dev, const char * dir, const char * fsname, NX_U32 flags);
NX_Error NX_FileSystemUnmount(const char * path);
NX_Error NX_FileSystemSync(void);
NX_Solt NX_FileOpen(const char * path, NX_U32 flags, NX_U32 mode);
NX_Error NX_FileClose(NX_Solt solt);
NX_Size NX_FileRead(NX_Solt solt, void * buf, NX_Size len);
NX_Size NX_FileWrite(NX_Solt solt, void * buf, NX_Size len);
NX_Error NX_FileIoctl(NX_Solt solt, NX_U32 cmd, void *arg);
NX_Offset NX_FileSetPointer(NX_Solt solt, NX_Offset off, int whence);
NX_Error NX_FileSync(NX_Solt solt);
NX_Error NX_FileSetMode(NX_Solt solt, NX_U32 mode);
NX_Error NX_FileGetStat(NX_Solt solt, NX_FileStatInfo * st);
NX_Solt NX_DirOpen(const char * name);
NX_Error NX_DirClose(NX_Solt solt);
NX_Error NX_DirRead(NX_Solt solt, NX_Dirent * dir);
NX_Error NX_DirResetPointer(NX_Solt solt);
NX_Error NX_DirCreate(const char * path, NX_U32 mode);
NX_Error NX_DirDelete(const char * path);
NX_Error NX_FileRename(const char * src, const char * dst);
NX_Error NX_FileDelete(const char * path);
NX_Error NX_FileAccess(const char * path, NX_U32 mode);
NX_Error NX_FileSetModeToPath(const char * path, NX_U32 mode);
NX_Error NX_FileGetStatFromPath(const char * path, NX_FileStatInfo * st);

#endif